﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Protocol.equipment;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Entities;

namespace Pixysoft.Weblications.Sango.Protocol
{
    class EquipmentFacade
    {
        public static List<eqm_equipment> ToEquipments(List<Entities.Eqm_Equipment> orms)
        {
            List<eqm_equipment> list = new List<eqm_equipment>();

            foreach (Entities.Eqm_Equipment orm in orms)
            {
                eqm_equipment body = ReflectionManager.CreatePojo<eqm_equipment>();
                list.Add(body);

                body.serialcode = orm.Equipmentserialcode;
                body.name = orm.Equipmentname;
                body.salesprice = orm.Equipmentprice;
                body.pointvalue = orm.Equipmentpointvalue;
                body.visiblelevel = orm.Visiblelevel;
                body.requiredlevel = orm.Requiredlevel;
                body.eqmtypecode = orm.Equipmenttypecode;
                body.placetypecode = orm.Placetypecode;
                body.gradetypecode = orm.Gradetypecode;
                body.description = orm.Description;
            }

            return list;
        }

        public static List<usr_equipment> ToEquipments(List<Entities.Usr_Equipment> orms, List<Entities.Usr_Equipmentextra> extras)
        {
            List<usr_equipment> list = new List<usr_equipment>();

            foreach (Entities.Usr_Equipment orm in orms)
            {
                list.Add(ToEquipment(orm, extras));
            }

            return list;
        }

        public static usr_equipment ToEquipment(Pixysoft.Weblications.Sango.Entities.Usr_Equipment orm, List<Entities.Usr_Equipmentextra> extras)
        {
            usr_equipment body = ReflectionManager.CreatePojo<usr_equipment>();

            Entities.Eqm_Equipment syseqm = EqmEquipmentHelper.searchBySerialcode(orm.Equipmentserialcode);

            body.name = orm.Equipmentname;
            body.requiredlevel = syseqm.Requiredlevel;
            body.serialcode = orm.Equipmentserialcode;
            body.pointvalue = orm.Equipmentpointvalue;
            body.primarycode = orm.Primarycode;
            body.iswearing = orm.Equipmentstatus == EquipmentStatusCollection.WEAR;
            body.level = orm.Gradelevel;
            body.officerprimarycode = orm.Officerprimarycode;
            body.isfragment = SettingHelper.BooleanValue(orm.Isfragment);
            body.fragmentcount = orm.Fragmentcount;

            body.upgradeprice = EquipmentFormular.getUpgradePrice(orm);
            body.downgradeprice = EquipmentFormular.getDowngradePrice(orm);
            body.sellprice = EquipmentFormular.getSellprice(syseqm, orm);

            body.eqmtypecode = syseqm.Equipmenttypecode;

            body.extras = ToExtras(orm.Primarycode, extras);

            return body;
        }

        private static List<eqm_extra> ToExtras(string primarycode, List<Entities.Usr_Equipmentextra> extras)
        {
            List<eqm_extra> list = new List<eqm_extra>();
            foreach (Entities.Usr_Equipmentextra extra in extras)
            {
                if (!extra.Equipmentprimarycode.Equals(primarycode, StringComparison.OrdinalIgnoreCase))
                    continue;

                Eqm_Extra sys = EqmExtraHelper.searchByCode(extra.Extraserialcode);

                eqm_extra body = ReflectionManager.CreatePojo<eqm_extra>();
                body.extravalue = extra.Extravalue;
                body.name = sys.Extraname;
                body.serialcode = sys.Extraserialcode;
                list.Add(body);
            }
            return list;
        }
    }
}
